<?php
if (isset($data['chi_tiet'])) {
  $ref_title = '';
  $sl_title = '';
  switch($data['type']) {
    case 'nguyen':
      $ref_title = 'Khách hàng';
      $sl_title = 'Số lượng';
      break;
    case 'hoi':
      $ref_title = 'Khách hàng';
      $sl_title = 'Được bảo hành';
      break;
  }
  ?>
  <div id="nhap-gia-title">Khớp lệnh phiếu sản phẩm <?php echo $data['phieu']->ma_phieu ?></div>
  <form method="post" action="<?php echo base_path() . 'san_pham/phieu_xuat_nhap/khop_phieu/' . $data['type'] . '/' . $data['phieu']->ma_phieu ?>" id="khop-phieu-vat-tu" class="<?php echo $data['type'] ?>">
    <div class="form-item form-select-wrapper nha-cung-cap-wrapper">
      <label><?php echo $ref_title ?>:</label>
      <?php if ($data['type'] !='hong' ) {?>
      <label class="data-text tncc-data-text"><?php echo $data['phieu']->ref . '[' . $data['phieu']->ma_ref . ']' ?></label>
      <?php }else {?>
      <label class="data-text tncc-data-text"><?php echo $data['phieu']->ref ?></label>
    <?php }?>
      <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <fieldset>
      <legend>Vật tư</legend>
      <div id="form-item-wrapper">
        <?php foreach ($data['chi_tiet'] as $vat_tu) { ?>
          <div class="form-item-row">
            <div class="form-item form-text-wrapper">
              <label>Tên sản phẩm:</label>
              <label class="data-text tvt-data-text" id="tvt-data-text-<?php echo $vat_tu->id ?>"><?php echo $vat_tu->ten_thanh_pham . ' [' . $vat_tu->ma_thanh_pham . ']' ?></label>
            </div>

            <div class="form-item form-text-wrapper">
              <label><?php echo $sl_title ?>:</label>
              <label class="data-text sl-data-text" id="sl-data-text-<?php echo $vat_tu->id ?>"><?php echo $vat_tu->so_luong ?></label>
            </div>
            <?php if ($data['type'] == 'hoi') { ?>
              <div class="form-item form-text-wrapper">
                <label>Mang đến:</label>
                <label class="data-text sl-data-text" id="sl-hoi-data-text-<?php echo $vat_tu->id ?>"><?php echo $vat_tu->so_luong_hoi ?></label>
              </div>
            <?php } ?>
            <div class="form-item">
              <input type="hidden" id="txt-ten-vat-tu-<?php echo $vat_tu->id ?>" value="<?php echo $vat_tu->ten_thanh_pham . ' [' . $vat_tu->ma_thanh_pham . ']' ?>" />
              <input type="hidden" id="txt-so-luong-<?php echo $vat_tu->id ?>" value="<?php echo $vat_tu->so_luong ?>" />
              <?php if ($data['type'] == 'hoi') { ?>
                <input type="hidden" id="txt-so-luong-hoi-<?php echo $vat_tu->id ?>" value="<?php echo $vat_tu->so_luong_hoi ?>" />
              <?php } ?>
              <input id="field-update-submit-<?php echo $vat_tu->id ?>" type="button" value="Thay đổi"
                     class="field-update-submit form-submit" onclick="show_update_phieu_form(<?php echo $vat_tu->id ?>)"/>
              <?php echo dt_get_ajax_loading('show-form-loading-'.$vat_tu->id) ?>
            </div>
            <div class="clear"></div>
          </div>
        <?php } ?>
      </div>
    </fieldset>
    <div class="form-item form-textarea-wrapper">
      <label for="ghi-chu">Ghi chú</label>
      <textarea id="ghi-chu" name="ghi_chu"
                class="form-textarea <?php echo ($_POST && (string) $_POST['error']['key'] == 'ghi_chu') ? 'error'
                  : '' ?>" cols="60" rows="5"><?php echo ($_POST) ? $_POST['ghi_chu'] : $data['phieu']->ghi_chu ?></textarea>

      <div class="description">Nhập tối đa 500 ký tự. Số lượng hiện thời: <span style="color: red;"
                                                                                id="ghi-chu-length">0</span></div>
    </div>
    <div id="submit-wrapper">
      <input id="submit" type="submit" onclick="return check_khop_phieu();" value="Khớp phiếu" class="form-submit"/>
    </div>
  </form>
  <div id='update-phieu-dialog' style="display: none;">
    <div class="form-item form-text-wrapper">
      <label for="txt-ten-vat-tu">Tên sản phẩm</label>
      <input id="txt-ten-vat-tu" type="text" value=""
             class="form-text form-autocomplete text txt-ten-vat-tu" autocomplete="OFF"/>
      <input class="autocomplete" type="hidden" id="txt-ten-vat-tu-autocomplete"
           value="<?php echo base_path() . 'san_pham/autocomplete' ?>" disabled="disabled"/>
      <div class="clear"></div>
    </div>
    <div class="form-item form-text-wrapper">
      <label for="txt-so-luong"><?php echo $sl_title ?></label>
      <input id="txt-so-luong" type="text" class="form-text text txt-so-luong" value="" autocomplete="OFF"/>
      <div class="clear"></div>
    </div>
    <?php if ($data['type'] == 'hoi') { ?>
      <div class="form-item form-text-wrapper">
        <label for="txt-so-luong-hoi">Mang đến</label>
        <input id="txt-so-luong-hoi" type="text" class="form-text text txt-so-luong-hoi" value="" autocomplete="OFF"/>
        <div class="clear"></div>
      </div>
    <?php } ?>
    <div class="form-item">
      <input type="hidden" id="txt-type" value="<?php echo $data['type'] ?>" />
      <input type="hidden" id="txt-id" value="" />
      <input type="button" onclick="update_phieu_chi_tiet_san_pham();" value="Cập nhật" class="form-submit"/>
      <input type="button" onclick="close_update_phieu_form();" value="Hủy bỏ" class="form-submit"/>
      <?php echo dt_get_ajax_loading('update-loading') ?>
    </div>
    <div class="clear"></div>
  </div>
<?php } ?>